Coverage Report

Created: 2025-05-07 21:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
D:\a\tools.proto\tools.proto\dynamic\src\field\codec\bytes.rs
Line
Count
Source
1
// Copyright (c) 2025, BlockProject 3D
2
//
3
// All rights reserved.
4
//
5
// Redistribution and use in source and binary forms, with or without modification,
6
// are permitted provided that the following conditions are met:
7
//
8
//     * Redistributions of source code must retain the above copyright notice,
9
//       this list of conditions and the following disclaimer.
10
//     * Redistributions in binary form must reproduce the above copyright notice,
11
//       this list of conditions and the following disclaimer in the documentation
12
//       and/or other materials provided with the distribution.
13
//     * Neither the name of BlockProject 3D nor the names of its contributors
14
//       may be used to endorse or promote products derived from this software
15
//       without specific prior written permission.
16
//
17
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29
use crate::field::codec::Codec;
30
31
pub struct ByteCodecLE;
32
pub struct ByteCodecBE;
33
34
impl Codec for ByteCodecLE {
35
62
    fn read(&self, buffer: &[u8]) -> u64 {
36
62
        let mut block: [u8; 8] = [0; 8];
37
62
        unsafe {
38
62
            std::ptr::copy_nonoverlapping(buffer.as_ptr(), block.as_mut_ptr(), buffer.len());
39
62
        }
40
62
        u64::from_le_bytes(block)
41
62
    }
42
43
17
    fn write(&self, buffer: &mut [u8], value: u64) {
44
17
        let block = value.to_le_bytes();
45
17
        unsafe {
46
17
            std::ptr::copy_nonoverlapping(block.as_ptr(), buffer.as_mut_ptr(), buffer.len());
47
17
        }
48
17
    }
49
}
50
51
impl Codec for ByteCodecBE {
52
7
    fn read(&self, buffer: &[u8]) -> u64 {
53
7
        let mut block: [u8; 8] = [0; 8];
54
7
        let offset = 8 - buffer.len();
55
7
        block[offset..buffer.len() + offset].copy_from_slice(buffer);
56
7
        u64::from_be_bytes(block)
57
7
    }
58
59
3
    fn write(&self, buffer: &mut [u8], value: u64) {
60
3
        let offset = 8 - buffer.len();
61
3
        let block = value.to_be_bytes();
62
3
        let motherfuckingrust = buffer.len();
63
3
        buffer.copy_from_slice(&block[offset..motherfuckingrust + offset]);
64
3
    }
65
}
66
67
#[cfg(test)]
68
mod tests {
69
    use crate::field::codec::bytes::ByteCodecLE;
70
    use crate::field::codec::Codec;
71
72
    #[test]
73
1
    fn basic() {
74
1
        let buffer = [0xFF, 0xFF, 0xFF, 0xFF];
75
1
        assert_eq!(ByteCodecLE.read(&buffer[0..4]), 0xFFFFFFFF);
76
1
    }
77
78
    #[test]
79
1
    fn other() {
80
1
        let mut buffer = [0, 0, 0, 0];
81
1
        ByteCodecLE.write(&mut buffer[0..1], 128);
82
1
        assert_eq!(ByteCodecLE.read(&buffer[0..1]), 128);
83
1
        ByteCodecLE.write(&mut buffer[1..3], 4242);
84
1
        assert_eq!(ByteCodecLE.read(&buffer[1..3]), 4242);
85
1
    }
86
}